import featuretools as ft
import pandas as pd

# 假设我们有两个简单的数据帧（DataFrame），一个是关于客户的，另一个是关于交易的
customers = pd.DataFrame({"customer_id": [1, 2, 3, 4], "age": [35, 45, 23, 56]})
transactions = pd.DataFrame({
    "transaction_id": [1, 2, 3, 4, 5],
    "customer_id": [1, 2, 3, 1, 2],
    "amount": [125.0, 200.0, 45.0, 75.0, 300.0],
    "transaction_time": ["2024-04-01", "2024-04-02", "2024-04-03", "2024-04-04", "2024-04-05"]
})

# 将数据帧转换为EntitySet
es = ft.EntitySet(id="sales_data")
es.add_dataframe(dataframe_name="customers", dataframe=customers, index="customer_id")
es.add_dataframe(dataframe_name="transactions", dataframe=transactions,
                 index="transaction_id")

# 定义关系
# relationship = ft.Relationship(es["customers"]["customer_id"], es["transactions"]["customer_id"])
# es.add_relationship(relationship)


# 添加关系
es = es.add_relationship(ft.Relationship(
    parent_column_name='customer_id',
    child_dataframe_name='sessions',
    child_column_name='customer_id'
))

es = es.add_relationship(ft.Relationship(
    parent_column_name='session_id',
    child_dataframe_name='transactions',
    child_column_name='session_id'
))



# 自动创建特征
features, feature_names = ft.dfs(entityset=es, target_entity="customers")

# 打印生成的特征
print("Generated Features:")
for name in feature_names:
    print(name)

# 打印特征矩阵
print("\nFeature Matrix:")
print(features.head())
